<template>
  <div class="icon-select">
    <el-select
      v-model="selectedValue"
      :placeholder="placeholder"
      class="icon-select__input"
      popper-class="icon-select__dropdown"
      filterable
      clearable
      @change="handleChange"
    >
      <!-- 自定义选中值的显示 -->
      <template #prefix>
        <el-icon v-if="selectedValue">
          <component :is="ElementPlusIcons[selectedValue]" />
        </el-icon>
      </template>

      <el-option v-for="name in iconList" :key="name" :label="name" :value="name">
        <div class="icon-option">
          <el-icon>
            <component :is="ElementPlusIcons[name]" />
          </el-icon>
          <span class="icon-name" style="margin-left: 6px">{{ name }}</span>
        </div>
      </el-option>
    </el-select>
  </div>
</template>

<script setup>
import { ref, watch } from 'vue'
import * as ElementPlusIcons from '@element-plus/icons-vue'

const props = defineProps({
  modelValue: String,
  placeholder: {
    type: String,
    default: '请选择图标',
  },
})

const emit = defineEmits(['update:modelValue'])

// 选中的值
const selectedValue = ref(props.modelValue)

// 图标列表
const iconList = Object.keys(ElementPlusIcons)

// 监听外部值变化
watch(
  () => props.modelValue,
  (val) => {
    selectedValue.value = val
  },
)

// 处理选择变化
const handleChange = (value) => {
  emit('update:modelValue', value)
}
</script>

<style lang="scss" scoped>
.icon-select {
  width: 100%;

  :deep(.el-select) {
    width: 100%;

    .el-input__wrapper {
      padding-left: 32px;
    }

    .el-input__prefix {
      left: 8px;
      font-size: 16px;
      color: var(--el-text-color-regular);
      display: flex;
      align-items: center;
      height: 100%;

      .el-icon {
        font-size: 16px;
      }
    }
  }

  .icon-option {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 8px 0;

    .el-icon {
      font-size: 18px;
      color: var(--el-text-color-regular);
      background-color: var(--el-fill-color-light);
      padding: 8px;
      border-radius: 4px;
      transition: all 0.2s;
    }

    .icon-name {
      color: var(--el-text-color-regular);
      font-size: 13px;
      padding-left: 4px;
    }
  }
}

:deep(.icon-select__dropdown) {
  .el-select-dropdown__item {
    height: auto;
    padding: 4px 12px;

    &.selected {
      .el-icon {
        color: var(--el-color-primary);
        background-color: var(--el-color-primary-light-9);
      }
      .icon-name {
        color: var(--el-color-primary);
        font-weight: bold;
      }
    }

    &:hover {
      background-color: var(--el-fill-color-light);

      .el-icon {
        color: var(--el-color-primary);
        background-color: var(--el-color-primary-light-9);
        transform: scale(1.1);
      }

      .icon-name {
        color: var(--el-color-primary);
      }
    }

    // 添加分隔线
    &:not(:last-child) {
      border-bottom: 1px solid var(--el-border-color-lighter);
    }
  }

  // 美化滚动条
  .el-select-dropdown__wrap {
    &::-webkit-scrollbar {
      width: 6px;
    }

    &::-webkit-scrollbar-thumb {
      background: var(--el-border-color-darker);
      border-radius: 3px;
    }

    &::-webkit-scrollbar-track {
      background: var(--el-fill-color-lighter);
    }
  }
}
</style>
